//
//  KSProgressHUD.swift
//  Merchant
//
//  Created by liuxm on 2017/8/24.
//  Copyright © 2017年 huanniu. All rights reserved.
//

import UIKit

/// 提示
class KSProgressHUD: UIView {
    
    private lazy var textLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: KSConstants.kScreenWidth - 60, height: 50))
        label.numberOfLines = 0
        label.lineBreakMode = NSLineBreakMode.byWordWrapping
        label.textColor = UIColor.white
        label.backgroundColor = UIColor.clear
        label.textAlignment = NSTextAlignment.center
        label.font = UIFont.systemFont(ofSize: 16)
        
        return label
    }()
    
    private lazy var textView: UIView = {
        let view = UIView()
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 7
        view.backgroundColor = UIColor(white: 0, alpha: 0.7)
        
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.clear
        self.alpha = 0
    }
    
    /// 显示消息
    ///
    /// - Parameters:
    ///   - text: 显示文字
    ///   - duration: 关闭间隔
    ///   - animated: 是否有动画
    func showMessage(text: String,duration: Int = 1,animated: Bool = true) {
        self.textLabel.text = text
        self.textLabel.sizeToFit()
        self.textView.addSubview(self.textLabel)
        self.addSubview(self.textView)
        let textW = self.textLabel.frame.size.width
        let textH = self.textLabel.frame.size.height
        
        KSConstants.kWindow?.addSubview(self)
        self.textView.frame = CGRect(x: (KSConstants.kScreenWidth - textW)/2 - 15, y: (KSConstants.kScreenHeight - textH)/2 - 15, width: textW + 30, height: textH + 30)
        self.textLabel.frame = CGRect(x: 15, y: 15, width: textW, height: textH)
        
        self.show(animated: animated) { (finish) in
            let delay = DispatchTime.now() + DispatchTimeInterval.seconds(duration)
            DispatchQueue.main.asyncAfter(deadline: delay, execute: {
                self.dismiss(animated: animated)
            })
        }
    }
    
    fileprivate func show(animated: Bool, completion: ((Bool) -> Swift.Void)? = nil) {
        self.alpha = 0
        if animated {
            UIView.animate(withDuration: 0.3, animations: { 
                self.alpha = 1
            }, completion: { (finished) in
                if completion != nil {
                    completion!(true)
                }
            })
        } else {
            self.alpha = 1
            completion!(true)
        }
    }
    
    fileprivate func dismiss(animated: Bool) {
        if animated {
            UIView.animate(withDuration: 0.3, animations: { 
                self.alpha = 0
            }, completion: { (finished) in
                if finished {
                    self.removeFromSuperview()
                }
            })
        } else {
            self.removeFromSuperview()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
